//
//  PersonalViewController.m
//  MyLittleBrother
//
//  Created by speeda on 17/7/29.
//  Copyright © 2017年 speeda. All rights reserved.
//

#import "PersonalViewController.h"
#import "UIImageView+WebCache.h"
#import "InfoViewController.h"
#import "OpenBoxView.h"
#import "CompetitionViewController.h"
#import "PrizeViewController.h"
#import "InviteFriendsViewController.h"
#import "MyDynamicViewController.h"
#import "SettingViewController.h"
#import "CoverView.h"
#import "OrdinaryView.h"

@interface PersonalViewController () <UITableViewDataSource,UITableViewDelegate,OrdinaryViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *topImageView;
@property (weak, nonatomic) IBOutlet UITableView *personalTableView;
@property (weak, nonatomic) IBOutlet UIImageView *headImgView;
@property (weak, nonatomic) IBOutlet UILabel *nickNameLabel;
@property (nonatomic, strong) NSArray *personalArray;
@property (nonatomic, strong) NSArray *subControllerArray;



@end

@implementation PersonalViewController

- (void)viewWillAppear:(BOOL)animated{
    
    //设置导航栏背景图片为一个空的image，这样就透明了
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
    
    //去掉透明后导航栏下边的黑边
    [self.navigationController.navigationBar setShadowImage:[[UIImage alloc] init]];
    
    //设置导航条主体颜色
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSURL *headImgUrl = [NSURL URLWithString:[userDefaults objectForKey:kHeadimg]];
    [self.headImgView sd_setImageWithURL:headImgUrl placeholderImage:[UIImage imageNamed:@"img_touxiang"]];
    self.nickNameLabel.text = [userDefaults objectForKey:kNickname];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavigationItem];
    
    [self initTableView];

}

- (void)initTableView {
    
    self.automaticallyAdjustsScrollViewInsets = NO;

    self.personalTableView.dataSource = self;
    self.personalTableView.delegate = self;
    self.personalTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.personalTableView.tableFooterView = [[UIView alloc] init];
    
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(topImageViewTap)];
    [self.topImageView addGestureRecognizer:tapRecognizer];
    
}


- (void)setNavigationItem {

    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setImage:[UIImage imageNamed:@"btn_more"] forState:UIControlStateNormal];
    [rightBtn sizeToFit];
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}

- (void)rightBtnAction {

    //初始化提示框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"分享" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    
        
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
    
}

- (void)topImageViewTap {

    InfoViewController *infoVc = [[InfoViewController alloc] init];
    [self.navigationController pushViewController:infoVc animated:YES];
}


#pragma mark -- 普通盒子
- (IBAction)ordinaryBoxAction:(id)sender {

   OrdinaryView *view = [OrdinaryView ordinaryViewFormXib];
    view.delegate = self;
}


#pragma mark -- 可打开盒子
- (IBAction)openBoxAction:(id)sender {
    
    [OpenBoxView openBoxViewFormXib];

}



#pragma mark -- OrdinaryViewDelegate
#pragma mark -- 去比武
- (void)ordinaryViewWithcompetitionButton {
    
    [self removeFromKeyWindow];

    CompetitionViewController *competitionVc = [[CompetitionViewController alloc] init];
    [self.navigationController pushViewController:competitionVc animated:YES];
}

- (void)removeFromKeyWindow {

    for (UIView *view in [UIApplication sharedApplication].keyWindow.subviews) {
        
        if ([view isKindOfClass:[OpenBoxView class]]) {
            
            [view removeFromSuperview];
        }
        if ([view isKindOfClass:[OrdinaryView class]]) {
            
            [view removeFromSuperview];
        }
        if ([view isKindOfClass:[CoverView class]]) {
            
            [view removeFromSuperview];
        }
        
    }
}


#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.personalArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *personalCell = @"personalCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:personalCell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:personalCell];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    cell.textLabel.text = self.personalArray[indexPath.row];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    UIViewController *ViewController = self.subControllerArray[indexPath.row];
    [self.navigationController pushViewController:ViewController animated:YES];
}


- (NSArray *)personalArray {

    if (!_personalArray) {
        _personalArray = @[@"奖品",@"邀请好友",@"我的动态",@"设置"];
    }
    
    return _personalArray;
}


- (NSArray *)subControllerArray {

    if (!_subControllerArray) {

        _subControllerArray = @[[[PrizeViewController alloc] init],[[InviteFriendsViewController alloc] init],[[MyDynamicViewController alloc] init],[[SettingViewController alloc] init]];
    }
    
    return _subControllerArray;
}


- (void)viewWillDisappear:(BOOL)animated{
    
    //    如果不想让其他页面的导航栏变为透明 需要重置
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:nil];
    
    [self.navigationController.navigationBar setTintColor:nil];
}

@end
